<template>
  <div>
    <!-- 加载动画 -->
    <!-- <van-loading
      class="loading"
      size="20px"
      type="spinner"
      vertical
      color="#1989fa"
      v-if="!bookmarkLists"
      >加载中...</van-loading
    > -->

    <div class="loading" v-if="!bookmarkLists">
      <div class="imgtexts">
        <p>数据加载中请稍等...</p>
      </div>
      <img src="../assets/imgs/loading.gif" alt="" />
    </div>

    <!-- 标题 -->
    <div class="settitle" v-if="bookmarkLists">
      <div class="setimg" @click="$router.back()">
        <img src="../assets/imgs/iconse/左箭头.png" alt="" />
      </div>
      <div class="text">
        <h2>{{ jcardLists[ind].title }}</h2>
        <p v-if="flag" class="throw" @click="throws">取关</p>
      </div>
    </div>

    <!-- 内容区域 -->
    <div class="box" @click.stop="chitchat" v-if="jcardLists && lists">
      <div class="title">
        <p>{{ jcardLists[ind].title }}</p>
        <div class="timg">
          <img :src="lists.srcs" alt="" />
        </div>
      </div>
      <div class="require">
        <p>{{ jcardLists[ind].place }}</p>
        <p v-for="(item, i) in jcardLists[ind].text" :key="i">
          {{ item }}
        </p>
      </div>
      <div class="payroll">
        <p>{{ jcardLists[ind].paric }}</p>
        <div class="time">发布于{{ lists.tiem }}</div>
      </div>
    </div>

    <!-- 描述模块 -->
    <div class="ttt" v-if="lists">
      <div class="box2" v-for="item in aas" :key="item">
        <div class="btitle">
          <p>职位描述</p>
          <div class="tx">
            <span>技术测试</span>
            <span>游戏</span>
          </div>
        </div>
        <div class="matter">
          <p>{{ lists.title }}:</p>
          <div class="boxs">
            <span v-for="(item, index) in lists.texts" :key="index">
              {{ item }}
            </span>
          </div>
        </div>
      </div>
    </div>

    <div class="btns"></div>
    <div
      :class="['btn', flag ? 'con' : '']"
      @click="flagFun"
      v-if="bookmarkLists && jcardLists"
    >
      <span v-if="!flag">点击收藏</span>
      <span v-if="flag">已收藏</span>
    </div>
  </div>
</template>

<script>
import { getJobwantJsonData } from "../api/jobwant.js";
import { getBookmarkJsonData } from "../api/bookmark.js";
export default {
  data() {
    return {
      jcardLists: null,
      bookmarkLists: null,
      lists: null,
      aas: [1, 2, 3],
      flag: false,
      ids: 0, //bookmarkLists内id
      ind: 0, //jcardLists内下标
    };
  },
  methods: {
    getCardListsFun() {
      getJobwantJsonData().then((data) => {
        this.jcardLists = data.jcardLists;
      });
    },
    getBookmarListsFun() {
      setTimeout(() => {
        getBookmarkJsonData().then((data) => {
          this.bookmarkLists = data.bookmark;
        });
      }, 1000);
    },
    getIdsFun() {
      this.ids = localStorage.getItem("ids");
      console.log("getIdsFun", this.ids);
    },
    getListsFun() {
      if (this.bookmarkLists && this.jcardLists) {
        this.ids = localStorage.getItem("ids");
        let index = this.bookmarkLists.findIndex((item) => item.id == this.ids);

        // 获取数据对应区域的数据
        this.lists = this.bookmarkLists[index];
        console.log("getListsFun", this.lists);
      }
    },

    // 跳转聊天页面
    chitchat() {
      if (this.flag) {
        this.$router.push("chitchat");
      } else if (!this.flag) {
        this.$toast("请收藏后进行对话");
      }
    },

    asd() {
      // 获取收藏列表

      // 获取记录
      if (this.bookmarkLists && this.jcardLists) {
        console.log("ids", this.ids);
        let oldcollect = JSON.parse(localStorage.getItem("collect"));
        let is = oldcollect.findIndex((item) => item.id == this.ids);
        if (is == -1) {
          return;
        }
        this.flag = oldcollect[is].flag;
      }
    },
    getIndFun() {
      if (this.bookmarkLists && this.jcardLists) {
        this.ids = localStorage.getItem("ids");
        let index = this.jcardLists.findIndex((item) => item.id == this.ids);
        this.ind = index;
        console.log("getIndFun1", index);
        console.log("jcardLists", this.jcardLists[index]);
      }
    },

    // 取消收藏
    throws() {
      this.flag = false;
      let oldcollect = JSON.parse(localStorage.getItem("collect"));
      let index = oldcollect.findIndex((item) => item.id == this.ids);
      // console.log(oldcollect);
      oldcollect.splice(index, 1);
      localStorage.setItem("collect", JSON.stringify(oldcollect));
      console.log(oldcollect);
      // this.$router.push("jobwant")
    },

    // 保存收藏信息
    flagFun() {
      // JSON.parse(list)是把字符串转数组 存的时候转字符串，取就转对象
      // JSON.stringify(newlist)是把数组转字符串 存的时候转字符串，取就转对象
      this.flag = true;
      if (!localStorage.getItem("collect")) {
        let collect = [
          {
            id: this.ids,
            flag: this.flag,
          },
        ];
        localStorage.setItem("collect", JSON.stringify(collect));
      } else if (localStorage.getItem("collect")) {
        let oldcollect = JSON.parse(localStorage.getItem("collect"));
        let collect = {
          id: this.ids,
          flag: this.flag,
        };
        oldcollect.push(collect);
        localStorage.setItem("collect", JSON.stringify(oldcollect));
        console.log("oldcollect", oldcollect);
      }
    },
  },
  created() {
    this.getCardListsFun();
    this.getBookmarListsFun();
    this.getListsFun();
    this.getIdsFun();
    this.getIndFun();
    this.asd();
  },
  watch: {
    bookmarkLists: function () {
      this.getListsFun();
      this.getIndFun();
      this.asd();
    },
    immediate: true,
    deep: true,
  },
  // ,
  // updated() {
  //   this.getListsFun();
  //   this.getIndFun();
  //   this.asd();
  // },
};
</script>

<style lang="scss" scoped>
// 加载动画
.loading {
  position: absolute;
  // top: 300px;
  top: 200px;
  bottom: 0px;
  right: 0px;
  left: 0px;
  text-align: center;
  margin: 0 auto;
  img {
    width: 50%;
    display: inline-block;
    box-shadow: 0px 0px 10px 10px #eee;
    border-radius: 20px;
  }
  .imgtexts {
    margin-bottom: 50px;
    animation: textloading 3s linear infinite;
    p {
      font-size: 35px;
      height: 35px;
      line-height: 35px;
    }
  }
}

// 动画帧
@keyframes textloading {
  0% {
    transform: translateX(-50%);
  }
  25% {
    transform: translateX(0%);
  }
  50% {
    transform: translateX(50%);
  }
  75% {
    transform: translateX(0%);
  }
  100% {
    transform: translateX(-50%);
  }
}

.settitle {
  height: 60px;
  display: flex;
  align-items: center;
  background-color: #fff;
  box-shadow: 0px 0px 0px 1px rgba($color: #cccc, $alpha: 0.4);
  .setimg {
    height: 18px;
    position: absolute;
    left: 15px;
    img {
      height: 100%;
      display: block;
    }
  }
  .text {
    margin: 0 auto;
    position: relative;
    .throw {
      position: absolute;
      height: 30px;
      font-size: 16px;
      line-height: 30px;
      top: -10px;
      right: -45px;
      bottom: 0px;
      margin: auto 0;
      color: #f46d37;
      font-weight: 600;
    }
  }
}

// 顶部第一模块
.box {
  background-color: #fefefe;
  margin: 0px 20px;
  margin-top: 30px;
  padding-bottom: 20px;
  border-radius: 10px;
  box-shadow: 0px 0px 8px 2px #f2f2f2;
  /*标题*/
  .title {
    height: 60px;
    line-height: 60px;
    font-size: 20px;
    color: #333;
    font-weight: 800;
    position: relative;
    box-sizing: border-box;
    padding: 0px 20px;
    .timg {
      height: 50px;
      width: 50px;
      position: absolute;
      overflow: hidden;
      border-radius: 50%;
      top: 0px;
      bottom: 0px;
      right: 20px;
      margin: auto 0;
      img {
        display: block;
        width: 100%;
      }
    }
  }

  /*要求*/
  .require {
    display: flex;
    font-size: 15px;
    margin-left: 10px;
    flex-wrap: wrap;
    p {
      padding: 0px 10px;
      margin: 5px 0px;
      border-right: 1px solid #ccc;
      &:nth-last-child(1) {
        border-right: 0px;
      }
    }
  }

  /*薪资*/
  .payroll {
    display: flex;
    font-size: 15px;
    margin: 0px 10px;
    margin-top: 20px;
    justify-content: space-between;
    p {
      padding: 0px 10px;
      font-size: 20px;
      color: #f46d37;
      font-weight: 600;
    }
    .time {
      padding: 0px 10px;
      font-size: 12px;
      color: #999;
    }
  }
}

// 模块二
.box2 {
  background-color: #fefefe;
  margin: 0px 20px;
  margin-top: 30px;
  padding-bottom: 20px;
  border-radius: 10px;
  box-shadow: 0px 0px 8px 2px #f2f2f2;
  /*标签*/
  .btitle {
    box-sizing: border-box;
    padding: 20px;
    p {
      height: 50px;
      line-height: 50px;
      font-weight: 600;
      font-size: 25px;
      color: #333;
    }
    .tx {
      font-size: 14px;
      color: #333;
      span {
        display: inline-block;
        padding: 5px 10px;
        margin: 0px 10px;
        background-color: rgba(204, 204, 204, 0.5);
      }
    }
  }

  /*内容要求*/
  .matter {
    box-sizing: border-box;
    padding: 20px;
    padding-top: 0px;
    // margin-bottom: 80px;
    p {
      font-size: 16px;
      height: 36px;
      line-height: 36px;
      color: #333;
    }
    .boxs {
      span {
        display: inline-block;
        font-size: 14px;
        height: 26px;
        line-height: 26px;
        color: #333;
        margin: 10px 0px;
        // transform: scale(0.8);
      }
    }
  }
}

// 收藏按钮
.btns {
  height: 100px;
}
.btn {
  position: fixed;
  width: 80%;
  height: 50px;
  line-height: 50px;
  font-size: 25px;
  text-align: center;
  background-color: #f46d37;
  border-radius: 25px;
  color: rgba(254, 254, 254, 0.8);
  right: 0px;
  left: 0px;
  bottom: 30px;
  margin: 0 auto;
  box-shadow: 0px 0px 8px 2px #f2f2f2;
}
.btn.con {
  background-color: #ccc;
  color: #333;
  font-weight: 800;
  pointer-events: none;
}
</style>